package com.itheima.sc.impl.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.order.OmsOrderReturnApplyApi;
import com.itheima.sc.mapper.order.OsmOrderReturnApplyMapper;
import com.itheima.sc.pojo.order.OmsOrderReturnApply;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@DubboService
public class OmsOrderReturnApplyImpl implements OmsOrderReturnApplyApi {

    @Autowired
    OsmOrderReturnApplyMapper osmOrderReturnApplyMapper;

    @Override
    public  Page<OmsOrderReturnApply> listOmsOrderReturnApply(Integer page, Integer size, Map<String, String> map) {
        Page<OmsOrderReturnApply> ipage = new Page<>(Convert.toLong(page), Convert.toLong(size));
        //订单列表
        LambdaQueryWrapper<OmsOrderReturnApply> wrapper = new LambdaQueryWrapper<>();
        //申请时间
        String createTime = map.get("createTime");
        if (ObjectUtil.isNotEmpty(createTime)) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parseCreateTime = simpleDateFormat.parse(createTime);
                long endtime = parseCreateTime.getTime() + Convert.toLong(1 * 24 * 3600 * 1000);
                Date date = new Date(endtime);
                wrapper.gt(OmsOrderReturnApply::getCreateTime, parseCreateTime);
                wrapper.lt(OmsOrderReturnApply::getCreateTime, date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //处理人员
        String handleMan = map.get("handleMan");
        if (ObjectUtil.isNotEmpty(handleMan)) {
            wrapper.eq(OmsOrderReturnApply::getStatus, handleMan);
        }
        //处理时间
        String handleTime = map.get("handleTime");
        if (ObjectUtil.isNotEmpty(handleTime)) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parseHandleTime = simpleDateFormat.parse(handleTime);
                long endtime = parseHandleTime.getTime() + Convert.toLong(1 * 24 * 3600 * 1000);
                Date date = new Date(endtime);
                wrapper.gt(OmsOrderReturnApply::getHandleTime, parseHandleTime);
                wrapper.lt(OmsOrderReturnApply::getHandleTime, date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //服务单号id
        String id = map.get("id");
        if (ObjectUtil.isNotEmpty(id)) {
            wrapper.like(OmsOrderReturnApply::getId, id);
        }
        //收货人姓名/号码
        String receiverKeyword = map.get("receiverKeyword");
        if (ObjectUtil.isNotEmpty(receiverKeyword)) {
            //判断是号码还是姓名
            Long isPhone = Convert.toLong(receiverKeyword);
            if (isPhone == null) {
                //是姓名
                wrapper.eq(OmsOrderReturnApply::getReturnName, receiverKeyword);
            } else {
                //是号码
                wrapper.eq(OmsOrderReturnApply::getReturnPhone, isPhone);
            }
        }
        //申请状态
        String status = map.get("status");
        if (ObjectUtil.isNotEmpty(status)) {
            Integer integer = Convert.toInt(status);
            wrapper.eq(OmsOrderReturnApply::getStatus, integer);
        }
        //从数据库返回集合
        List<OmsOrderReturnApply> OmsOrderReturnApplys = osmOrderReturnApplyMapper.selectList(wrapper);

        ipage = osmOrderReturnApplyMapper.selectPage(ipage, wrapper);
        return ipage;
    }

    @Override
    public Boolean deletByIds(ArrayList<Long> ids) {
       //当选择的集合中有待处理和处理中，都不能删除
        for (Long id : ids) {
            List<OmsOrderReturnApply> omsOrderReturnApplies = osmOrderReturnApplyMapper.selectBatchIds(ids);
            List<Integer> status = CollUtil.getFieldValues(omsOrderReturnApplies, "status", Integer.class);
            if (status.contains(0) || status.contains(1)) {
                return false;
            }
        }
        //只有当退货申请已经完成或者已经拒绝才能删除
        osmOrderReturnApplyMapper.deleteBatchIds(ids);
        return true;

    }

    @Override
    public Long updateStatus(Long id, Map<String, String> map) {
        LambdaUpdateWrapper<OmsOrderReturnApply>wrapper=new LambdaUpdateWrapper<>();
        OmsOrderReturnApply updateOmsOrderReturnApply = new OmsOrderReturnApply();
        //对象的条件
        wrapper.eq(OmsOrderReturnApply::getId,id);
        //修改值
        updateOmsOrderReturnApply.setCompanyAddressId(Convert.toLong(map.get("companyAddressId")));
        updateOmsOrderReturnApply.setHandleMan(map.get("handleMan"));
        updateOmsOrderReturnApply.setHandleNote(map.get("handleNote"));
        if (ObjectUtil.isNotEmpty(map.get("id"))){
            updateOmsOrderReturnApply.setId(Convert.toLong(map.get("id")));
        }
        updateOmsOrderReturnApply.setReceiveMan(map.get("receiveMan"));
        updateOmsOrderReturnApply.setReceiveNote(map.get("receiveNote"));
        updateOmsOrderReturnApply.setReturnAmount(Convert.toDouble(map.get("returnAmount")));
        updateOmsOrderReturnApply.setStatus(Convert.toInt(map.get("status")));
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (map.get("status").equals("1")||map.get("status").equals("3")){
                //处理设置
                updateOmsOrderReturnApply.setHandleTime(
                        simpleDateFormat.parse(
                                simpleDateFormat.format(
                                        new Date(System.currentTimeMillis())
                                )
                        )
                );
            }else if (map.get("status").equals("2")){
                //收货设置
                updateOmsOrderReturnApply.setReceiveTime(
                        simpleDateFormat.parse(
                                simpleDateFormat.format(
                                        new Date(System.currentTimeMillis())
                                )
                        )
                );
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //修改信息
        osmOrderReturnApplyMapper.update(updateOmsOrderReturnApply,wrapper);
        OmsOrderReturnApply omsOrderReturnApply = osmOrderReturnApplyMapper.selectById(id);
        return omsOrderReturnApply.getOrderId();
    }

    @Override
    public OmsOrderReturnApply getById(Long id) {
        //
        OmsOrderReturnApply omsOrderReturnApply = osmOrderReturnApplyMapper.selectById(id);
        return omsOrderReturnApply;
    }


}
